package houseHold;

import grammer.components.ObjectEnum;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;
import java.util.Vector;

import sensor.Sensor;
import sensor.SensorType;
import consumption.ConsumptionType;

/**
 * 
 * A room in the house - contains a list of all objects within it;
 * 
 * @author Minodor
 * 
 */
public class Room {
	private int ID;
	private HashMap<String, HouseHoldObject> items;
	private String name;
	private Point2D origin;
	private HashMap<String, Sensor> sensors;
	private RoomType type;

	private int width, height, length;

	private boolean windowStatus;

	public Room() {
		items = new HashMap<String, HouseHoldObject>();
		sensors = new HashMap<String, Sensor>();
		width = 0;
		height = 0;
		length = 0;
		type = RoomType.ROOM;
		name = "NewRoom";
		origin = new Point2D();
		windowStatus = false;
	}

	public void addItem(HouseHoldObject hho) {
		items.put(hho.getName(), hho);
		hho.setLocation_r(this);
	}

	public void addSensor(Sensor s) {
		sensors.put(s.getSensorName(), s);
	}

	public int getHeight() {
		return height;
	}

	public int getID() {
		return ID;
	}

	public HouseHoldObject getItem(String itemName) {
		itemName = itemName.replace('_', ' ');
		return items.get(itemName);
	}

	public HouseHoldObject getItemByType(ObjectEnum e) {
		Set<String> keySet = items.keySet();
		Iterator<String> it = keySet.iterator();
		while (it.hasNext()) {
			String current = it.next();
			if ((items.get(current).getType().toString().replace('_', ' '))
					.equalsIgnoreCase(e.toString().replace('_', ' ')))
				return items.get(current);
		}
		return null;// finish me
	}

	public int getItemCount() {
		return items.size();
	}

	public final HashMap<String, HouseHoldObject> getItemList() {
		return items;
	}

	public int getLength() {
		return length;
	}

	public String getName() {
		return name;
	}

	public Point2D getOrigin() {
		return origin;
	}

	public Sensor getSensor(ConsumptionType consumptionType) {
		Set<String> keySet = getSensors().keySet();
		Iterator<String> it = keySet.iterator();
		while (it.hasNext()) {
			String aux = it.next();
			if (getSensors().get(aux).getSensorRecord() == consumptionType) {
				return getSensors().get(aux);
			}
		}
		return null;
	}

	public Sensor getSensor(String sensorName) {
		return sensors.get(sensorName);
	}

	public Vector<Sensor> getSensorList(ConsumptionType consumptionType) {
		Vector<Sensor> list = new Vector<Sensor>();
		Set<String> keySet = getSensors().keySet();
		Iterator<String> it = keySet.iterator();
		while (it.hasNext()) {
			String aux = it.next();
			if (getSensors().get(aux).getSensorRecord() == consumptionType) {
				list.add(getSensors().get(aux));
			}
		}
		return list;
	}

	public HashMap<String, Sensor> getSensors() {
		return sensors;
	}

	public RoomType getType() {
		return type;
	}

	public int getWidth() {
		return width;
	}

	public boolean hasElectricSensor() {
		Set<String> keySet = getSensors().keySet();
		Iterator<String> it = keySet.iterator();
		while (it.hasNext()) {
			if (getSensors().get(it.next()).getType() == SensorType.ELECTRIC_SENSOR)
				return true;
		}
		return false;
	}

	public boolean hasGasSensor() {
		Set<String> keySet = getSensors().keySet();
		Iterator<String> it = keySet.iterator();
		while (it.hasNext()) {
			if (getSensors().get(it.next()).getType() == SensorType.GAS_SENSOR)
				return true;
		}
		return false;
	}

	public boolean hasWaterSensor() {
		Set<String> keySet = getSensors().keySet();
		Iterator<String> it = keySet.iterator();
		while (it.hasNext()) {
			if (getSensors().get(it.next()).getType() == SensorType.WATER_SENSOR)
				return true;
		}
		return false;
	}

	public boolean isWindowStatus() {
		return windowStatus;
	}

	public void overwrite(String oldKey, String newKey,
			HouseHoldObject newObject) {
		items.remove(oldKey);
		items.put(newKey, newObject);
	}

	public void removeItem(String key) {
		this.items.remove(key);

	}

	public void removeSensor(String itemName) {
		sensors.remove(itemName);
	}

	public void setHeight(int height) {
		this.height = height;
	}

	public void setID(int iD) {
		ID = iD;
		if (!name.contains("#"))
			this.name = name + "#" + ID;
		else
			this.name = name.substring(0,name.indexOf('#')+1 ) + ID;
	}

	public void setItem(String itemName, HouseHoldObject newItem) {
		items.put(itemName, newItem);
	}

	public void setItemState(boolean active, String itemName) {
		itemName = itemName.replace('_', ' ');
		items.get(itemName).setActive(active);
	}

	public void setLength(int length) {
		this.length = length;
	}

	public void setName(String name) {
		this.name = name;
	}

	public void setOrigin(Point2D origin) {
		this.origin = origin;
	}

	public void setSensors(HashMap<String, Sensor> sensors) {
		this.sensors = sensors;
	}

	public void setType(RoomType type) {
		this.type = type;
	}

	public void setWidth(int width) {
		this.width = width;
	}

	public void setWindowStatus(boolean windowStatus) {
		this.windowStatus = windowStatus;
	}

	@Override
	public String toString() {
		String room = "\nRoom: \n";
		room = room.concat("\nName = " + name);
		room = room.concat("\nType = " + type.toString());
		room = room.concat("\nSize = ( l=" + length + " w=" + width + " h="
				+ height + " )");
		room = room.concat("\nOrigin = " + origin.toString());
		Set<String> keySet = items.keySet();
		Iterator<String> it = keySet.iterator();
		while (it.hasNext()) {
			room = room.concat(items.get(it.next()).print());
		}
		keySet = sensors.keySet();
		it = keySet.iterator();
		while (it.hasNext()) {
			room = room.concat(sensors.get(it.next()).print());
		}
		room.concat("\n");
		System.out.println("KEY SET= " + keySet);
		return room;
	}

	public void turnEverythingOff() {
		Set<String> keySet = items.keySet();
		Iterator<String> it = keySet.iterator();
		while (it.hasNext()) {
			items.get(it.next()).setActive(false);
		}
	}
	
	private Vector<Sensor> temp;
	private Vector<Sensor> light;
	private Vector<Sensor> window;
	private Vector<Sensor> presence;
	private Vector<Sensor> electric;
	private Vector<Sensor> gas;
	private Vector<Sensor> water;
	
	private boolean hasGeneral;
	
	public boolean hasGeneral()
	{
		return hasGeneral;
	}
	
	public void builSensorLists()
	{
			hasGeneral=false;
		 temp=new Vector<Sensor>();
		 gas=new Vector<Sensor>();
		 water=new Vector<Sensor>();
		 electric=new Vector<Sensor>();
		presence=new Vector<Sensor>();
		light=new Vector<Sensor>();
		window=new Vector<Sensor>();
		 Set<String> keySet = sensors.keySet();
			Iterator<String> it = keySet.iterator();
			while (it.hasNext()) {
				Sensor s=sensors.get(it.next());
				switch(s.getType())
				{
				case WINDOW_SENSOR:
					window.add(s);
					hasGeneral=true;
					break;
				case GAS_SENSOR:
					gas.add(s);
					break;
				case WATER_SENSOR:
					water.add(s);
					break;
				case ELECTRIC_SENSOR:
					electric.add(s);
					break;
				case TEMPERATURE_SENSOR:
					temp.add(s);
					hasGeneral=true;
					break;
				case LIGHT_SENSOR:
					light.add(s);
					hasGeneral=true;
					break;
				case PR_SENSOR:
					presence.add(s);
					hasGeneral=true;
					break;
				}
			}
	}
	
	
	public Vector<Sensor> getTempSensors()
	{
		if(temp.size()>0)
			return temp;
			return null;
	}
	
	public Vector<Sensor> getLightSensors()
	{
		if(light.size()>0)
			return light;
			return null;
	}
	public Vector<Sensor> getWindowSensors()
	{
		if(window.size()>0)
			return window;
			return null;
	}
	public Vector<Sensor> getPresenceSensors()
	{
		if(presence.size()>0)
			return presence;
			return null;
	}
	
	public Vector<Sensor> getGasSensors()
	{
		if(gas.size()>0)
			return gas;
			return null;
	}
	
	public Vector<Sensor> getElectricSensors()
	{
		if(electric.size()>0)
			return electric;
			return null;
	}
	
	public Vector<Sensor> getWaterSensors()
	{
		if(water.size()>0)
			return water;
			return null;
	}
	
	
	
	

}
